const HashTable = require("../template")

function testHashTableBasic() {
  const ht = new HashTable()

  console.log(
    "1. 基本插入和获取测试:",
    (() => {
      ht.set('name', 'John')
      ht.set('age', 25)
      ht.set('city', 'New York')
      return ht.get('name') === 'John' &&
             ht.get('age') === 25 &&
             ht.get('city') === 'New York' &&
             ht.get('country') === undefined
    })()
  )

  console.log(
    "2. 基本删除测试:",
    (() => {
      ht.delete('age')
      return ht.get('age') === undefined &&
             ht.get('name') === 'John' &&
             ht.get('city') === 'New York'
    })()
  )

  console.log(
    "3. 检查键是否存在测试:",
    (() => {
      return ht.has('name') === true &&
             ht.has('age') === false &&
             ht.has('city') === true &&
             ht.has('country') === false
    })()
  )

  console.log(
    "4. 获取所有键测试:",
    (() => {
      const keys = ht.keys()
      return keys.includes('name') &&
             keys.includes('city') &&
             keys.length === 2
    })()
  )

  console.log(
    "5. 获取所有值测试:",
    (() => {
      const values = ht.values()
      return values.includes('John') &&
             values.includes('New York') &&
             values.length === 2
    })()
  )

  console.log(
    "6. 获取所有键值对测试:",
    (() => {
      const entries = ht.entries()
      return entries.length === 2 &&
             entries.some(([key, value]) => key === 'name' && value === 'John') &&
             entries.some(([key, value]) => key === 'city' && value === 'New York')
    })()
  )

  console.log(
    "7. 哈希表大小测试:",
    (() => {
      return ht.size() === 2 &&
             ht.isEmpty() === false
    })()
  )

  console.log(
    "8. this上下文哈希表测试:",
    (() => {
      const htObj = {
        multiplier: 2,
        processHashTable: function(entries) {
          const ht = new HashTable()
          entries.forEach(([key, value]) => ht.set(key, value))
          return ht.size() * this.multiplier
        }
      }
      return htObj.processHashTable([['a', 1], ['b', 2]]) === 4 // 2*2=4
    })()
  )

  console.log(
    "9. 复杂对象this绑定哈希表测试:",
    (() => {
      const htConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(entries) {
          const ht = new HashTable()
          entries.forEach(([key, value]) => ht.set(key, value))
          return ht.size() >= this.config.threshold
        }
      }
      return htConfigObj.processWithConfig([['a', 1], ['b', 2], ['c', 3]]) === true
    })()
  )

  console.log(
    "10. 哈希表一致性测试:",
    (() => {
      const consistencyHt = new HashTable()
      consistencyHt.set('key1', 'value1')
      consistencyHt.set('key2', 'value2')
      const size1 = consistencyHt.size()
      consistencyHt.delete('key1')
      const size2 = consistencyHt.size()
      return size1 === 2 && size2 === 1
    })()
  )

  console.log(
    "11. 哈希表边界测试:",
    (() => {
      const boundaryHt = new HashTable()
      boundaryHt.set('', 'empty')
      return boundaryHt.get('') === 'empty' &&
             boundaryHt.has('') === true &&
             boundaryHt.size() === 1
    })()
  )

  console.log(
    "12. 哈希表性能测试:",
    (() => {
      const perfHt = new HashTable()
      const start = performance.now()
      for (let i = 0; i < 1000; i++) {
        perfHt.set(`key${i}`, `value${i}`)
      }
      for (let i = 0; i < 1000; i++) {
        perfHt.get(`key${i}`)
      }
      const end = performance.now()
      return perfHt.size() === 1000 && (end - start) < 1000
    })()
  )
}

testHashTableBasic()
